package helpers.other
{
	import define.Label;
	
	import flash.events.Event;
	import flash.utils.clearInterval;
	import flash.utils.getTimer;
	import flash.utils.setInterval;
	
	import mx.containers.Canvas;
	import mx.core.Application;
	import mx.events.FlexEvent;

	public class HScrollLabel extends Canvas
	{
		private var _allLabels:Array/*{label:label,interval:interval,duration:duration,intervalId:intervalId}*/ = [];
		
		/**
		 * 等待显示的队列
		 */
		private var _readyToShow:Array = [];
		
		private var _curLabel:Label;
		
		private var _curObj:Object;
		
		private var renderId:int;
		
		private var _frameElpse:int;
		
		public function HScrollLabel()
		{
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,createdHandler);
		}
		private function createdHandler(event:FlexEvent):void
		{
			setInterval(render,15);
			_curLabel = new Label();
			this.width = 150;
			this.height = 25;
			this.addChild(_curLabel);
			this.horizontalScrollPolicy = 'off';
		}
		
		/**
		 * 增加一条信息 
		 * @param label 信息内容
		 * @param interval 滚动间隔
		 * @param duration 总滚动时间
		 * 
		 */
		public function addLabel(label:String, interval:int = 5000, duration:int = 15000):void
		{
			if(isLabelIn(label)){
				var obj:Object = getLabelObj(label);
				obj.interval = interval;
				obj.duration = duration;
				clearInterval(obj.intervalId);
				obj.intervalId = setInterval(enter,interval,obj);
				enter(obj);
			}
			else
			{
				var obj:Object = {label:label,interval:interval,duration:duration};
				obj.intervalId = setInterval(enter,interval,obj);
				_allLabels.push(obj);
				enter(obj);
			}
		}
		
		public function isLabelIn(label:String):Boolean
		{
			for each(var obj:Object in _allLabels){
				if(obj.label == label)
					return true;
			}
			return false;
		}
		
		protected function getLabelObj(label:String):Object
		{
			for each(var obj:Object in _allLabels){
				if(obj.label == label)
					return obj;
			}
			return false;
		}
		
		private function enter(obj:Object):void
		{
			if(!obj.hasOwnProperty('curTime') || obj.duration <= 0 //如果是第一次 或者 duration 小于等于零 就插入
				|| (obj.hasOwnProperty('curTime') && obj.curTime<obj.duration)){//还没有到达滚动的总时长
				var index:int = _readyToShow.indexOf(obj);
				if(index>-1){
					_readyToShow.splice(index,1);
				}
				_readyToShow.push(obj);
				if(obj != _readyToShow[0] && obj == _curObj)//当前插入的滚动文字不是第一个 并且正在滚动的时候，进行强制停止处理
					stopMoveLabel();
			} else {
				var index:int = _readyToShow.indexOf(obj);
				if(index>-1){
					_readyToShow.splice(index,1);
				}
			}
		}
		
		private var lasttime:int = 0;
		
		protected function render():void
		{
			if(lasttime>0){
				_frameElpse = getTimer()-lasttime;
			}
			lasttime = getTimer();
			var obj:Object = _readyToShow[0];
			if(obj){
				if(_curObj == obj && _curObj){//如果相同进行左移动
					this._curLabel.x -= Number(_frameElpse)/50;
					if(this._curLabel.width-this._curLabel.x >= 2*this._curLabel.width){
						stopMoveLabel();
					}
				} else {
					startMoveLabel(obj);						
				}
				if(_curObj){//记录时间
					_curObj.curTime = int(_curObj.curTime)+_frameElpse;
				}
			}
		}
		
		private function startMoveLabel(obj:Object):void
		{
			this._curLabel.x = this.width;
			this._curLabel.htmlText = obj.label;
			this._curLabel.validateNow();
			this._curLabel.width = this._curLabel.textWidth;
			this._curLabel.height = this.height;
			_curObj = obj;
			trace('start',obj.label);
		}
		
		private function stopMoveLabel():void
		{
			this._curLabel.htmlText = '';
			trace('stop', _curObj.label);
			_curObj = null;
		}
	}
}